﻿using System;
using System.Windows.Forms;
using System.Threading;
using Business;
using Business.Entities;
using System.Drawing;
using Business.Logic;

namespace GogTasks
{
    public partial class ErrorForm : Form
    {
        private ErrorPackage _errorPackage;
        public ErrorForm(ErrorPackage errorPackage)
        {
            InitializeComponent();
            _errorPackage = errorPackage;
            ErrorReportButton.Visible = _errorPackage != null;
        }

        public void ShowDialog(string text, string caption)
        {
            var bounds = Screen.PrimaryScreen.Bounds;
            Left = (bounds.Width - Width) / 2;
            Top = (bounds.Height - Height) / 2;
            DetailsTextBox.Text = text;
            CaptionLabel.Text = caption;
            ShowDialog();
        }

        /// <summary>
        /// Creates error message based on exceptions
        /// </summary>
        public void ShowDialog(Exception ex)
        {
            string text = "";
            string caption = "Communication error";
            if (ex.Message.Contains("remote name") || ex.Message.Contains("not establish connection"))
            {
                text = ex.Message + Environment.NewLine + "Please check your internet connection.";
                MessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            else
            {
                text = ex.Message + Environment.NewLine + Environment.NewLine + "Stack trace:" + Environment.NewLine + ex.StackTrace;
                var bounds = Screen.PrimaryScreen.Bounds;
                Left = (bounds.Width - Width) / 2;
                Top = (bounds.Height - Height) / 2;
                DetailsTextBox.Text = text;
                CaptionLabel.Text = caption;
                ShowDialog();
            }
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ErrorReportButton_Click(object sender, EventArgs e)
        {
            SendReportAsync();
        }

        private void SendReportAsync()
        {
            var res = MessageBox.Show("This will send an error report with debug information to Nilsen Nettutvikling. " +
                "The report is anonymous and does not contain any information revealing your credentials.",
                "Confirm submission", MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            if (res == DialogResult.OK)
            {
                var t1 = new Thread(SendReport);
                t1.Start();
                SendingLabel.Visible = true;
            }
        }

        private void SendReport_Complete()
        {
            SendingLabel.Visible = false;
            if (_sendOk)
            {
                MessageBox.Show("Submission of error report complete.", "Done");
                Close();
            }
            else
            {
                MessageBox.Show("Error sending error report. (Ironic, huh?).\r\n" + _sendEx.Message, "Error");
            }
        }

        bool _sendOk = false;
        Exception _sendEx = null;

        private void SendReport()
        {
            Cursor.Current = Cursors.WaitCursor;
            // Start

            try
            {
                string errorData = GetErrorDataString();
                var ws = new LicenseServer.LicenseService();
                string version = Context.GetAssemblyVersion();
                ws.PostError("GogTasks mobile " + version, errorData);
                _sendOk = true;
            }
            catch (Exception ex)
            {
                _sendEx = ex;
            }

            // Done
            Invoke(new ThreadStart(SendReport_Complete));
            Cursor.Current = Cursors.Default;
        }

        private string GetErrorDataString()
        {
            string data = ObjectStreamer.ObjectToString(_errorPackage);
            return data;
        }

        private void ErrorForm_Paint(object sender, PaintEventArgs e)
        {
            var pen1 = new Pen(Color.Black);
            var pen2 = new Pen(Color.Gray);
            e.Graphics.DrawRectangle(pen2, 0, 0, Width - 1, Height - 1);
            e.Graphics.DrawRectangle(pen1, 1, 1, Width - 2, Height - 2);
        }

    }
}